﻿using FRMS.Helper;
using FRMS.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace FRMS.DAL
{
    public class DataDAL
    {
        public bool ClearAll(int projectid)
        {
            string strSql = string.Format(@"DELETE FROM building_info WHERE projectID={0};
                                            DELETE FROM controllerstate_record WHERE DeviceID in(SELECT ID FROM device_info WHERE projectID={0}); 
                                            DELETE FROM device_info WHERE projectID={0};
                                            DELETE FROM device_record WHERE DeviceID in(SELECT ID FROM device_info WHERE projectID={0});
                                            DELETE FROM elevator_info WHERE projectID={0};
                                            DELETE FROM owner_info WHERE projectID={0};
                                            DELETE FROM room_info WHERE projectID={0};
                                            DELETE FROM voucher_info WHERE projectID={0};
                                            DELETE FROM workmodestate_record WHERE DeviceID in(SELECT ID FROM device_info WHERE projectID={0})", projectid);
            return SqlHelper<BuildInfo>.SqlExecute(strSql);
        }
        public bool ClearOwner(int projectid)
        {
            string strSql = string.Format(@"DELETE FROM owner_info WHERE projectID={0};
                                           DELETE FROM voucher_info WHERE projectID={0}", projectid);
            return SqlHelper<BuildInfo>.SqlExecute(strSql);
        }
        public bool ClearRoom(int projectid)
        {
            string strSql = string.Format(@"DELETE FROM room_info WHERE projectID={0};
                                            DELETE FROM owner_info WHERE projectID={0};
                                            DELETE FROM voucher_info WHERE projectID={0}", projectid);
            return SqlHelper<BuildInfo>.SqlExecute(strSql);
        }
        public bool ClearElevator(int projectid)
        {
            string strSql = string.Format(@"DELETE FROM controllerstate_record WHERE DeviceID in(SELECT ID FROM device_info WHERE projectID={0}); 
                                            DELETE FROM device_info WHERE projectID={0};
                                            DELETE FROM device_record WHERE DeviceID in(SELECT ID FROM device_info WHERE projectID={0});
                                            DELETE FROM elevator_info WHERE projectID={0};
                                            DELETE FROM voucher_info WHERE projectID={0};
                                            DELETE FROM workmodestate_record WHERE DeviceID in(SELECT ID FROM device_info WHERE projectID={0})", projectid);
            return SqlHelper<BuildInfo>.SqlExecute(strSql);
        }

        public bool AddErrorLog(ErrorLog errorLog)
        {
            string strSql = string.Format(@"INSERT INTO errorlog(Operation,ErrorMsg,UpdateTime)VALUES('{0}','{1}','{2}')",
                errorLog.Operation, errorLog.ErrorMsg, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            return SqlHelper<BuildInfo>.SqlExecute(strSql);
        }

        public List<ErrorLog> QueryErrorLog(ErrorLog errorLog)
        {
            string strSql = "SELECT * FROM errorlog";
            string sqlwhere = string.Empty;

            if (!string.IsNullOrEmpty(errorLog.StartTime))
            {
                sqlwhere += " UpdateTime>='" + errorLog.StartTime + "' AND";
            }
            if (!string.IsNullOrEmpty(errorLog.EndTime))
            {
                sqlwhere += " DATE(UpdateTime) <=DATE('" + errorLog.EndTime + "') AND";
            }

            if (sqlwhere.Length > 0)
            {
                strSql += " where" + sqlwhere.Substring(0, sqlwhere.Length - 4);
            }
            strSql += " ORDER BY UpdateTime DESC";
            if (errorLog.Recordhead > -1 && errorLog.Recordtail > 0)
            {
                strSql += " LIMIT " + errorLog.Recordhead + "," + errorLog.Recordtail;
            }
            return SqlHelper<ErrorLog>.SqlList(strSql);
        }

        public int QueryErrorLogCount(ErrorLog errorLog)
        {
            string strSql = "SELECT COUNT(1) FROM errorlog";
            string sqlwhere = string.Empty;

            if (!string.IsNullOrEmpty(errorLog.StartTime))
            {
                sqlwhere += " UpdateTime>='" + errorLog.StartTime + "' AND";
            }
            if (!string.IsNullOrEmpty(errorLog.EndTime))
            {
                sqlwhere += " DATE(UpdateTime) <=DATE('" + errorLog.EndTime + "') AND";
            }

            if (sqlwhere.Length > 0)
            {
                strSql += " where" + sqlwhere.Substring(0, sqlwhere.Length - 4);
            }
            strSql += " ORDER BY UpdateTime DESC";

            return SqlHelper<ErrorLog>.SqlScalar(strSql);
        }
    }
}
